﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LifeShare1.Media;
using LifeShare1.Models;
namespace LifeShare1.Controllers
{
    public class MovieController : Controller
    {
        //
        // GET: /Movie/
        private MediaService myService = new MediaService();
        public ActionResult MovieIndex(int page = 0)
        {
            int pagesize = 10;
            if (page * pagesize > myService.GetAllMovieInfoes().Count())
                page -= 1;
            if (page <= 0)
                page = 1;
            ViewData["page"] = page;
            MovieInfo[] model = myService.GetAllMovieInfoes();
            return View(model);
        }
        public ActionResult MovieInfoDetails(int resourceID)
        {
            MovieInfo Movie = myService.GetMovieInfo(resourceID);
            ViewData["MyTitle"] = Movie.title;
            ViewData["MyAuthor"] = Movie.author;
            ViewData["MyDirector"] = Movie.director;
            ViewData["MyImageURL"] = Movie.imageURL;
           ViewData["MyRatingAverage"] = Movie.ratingAverage;
            ViewData["MyNumRaters"] = Movie.numRaters;
            ViewData["MySummary"] = Movie.summary;
            ViewData["MyYear"] = Movie.year;
            ViewData["MyMovieDuration"] = Movie.movieDuration;
            ViewData["MyResourceID"] = Movie.ResourceID;
            Comment[] comments = myService.GetCommentList(resourceID);
            if (comments.Count() == 0)
            {
                myService.AddComment(-1, resourceID, "");
            }
            comments = myService.GetCommentList(resourceID);
            return View(comments);
        }
        public ActionResult AddMovieComment(int resourceID)
        {
            MovieInfo Movie = myService.GetMovieInfo(resourceID);
            ViewData["MyTitle"] = Movie.title;
            ViewData["MyAuthor"] = Movie.author;
            ViewData["MyDirector"] = Movie.director;
            ViewData["MyImageURL"] = Movie.imageURL;
            ViewData["MyRatingAverage"] = Movie.ratingAverage;
            ViewData["MyNumRaters"] = Movie.numRaters;
            ViewData["MySummary"] = Movie.summary;
            ViewData["MyYear"] = Movie.year;
            ViewData["MyMovieDuration"] = Movie.movieDuration;
            ViewData["MyResourceID"] = Movie.ResourceID;
            Comment[] comments = myService.GetCommentList(resourceID);
            if (comments.Count() == 0)
            {
                myService.AddComment(-1, resourceID, "");
            }
            comments = myService.GetCommentList(resourceID);
            return View(comments);
        }
        [AcceptVerbs(HttpVerbs.Post), ActionName("AddMovieComment")]
        public ActionResult AddCommentClick(int userID, int resourceID, string NewComment)
        {
            if (NewComment == "")
            {
                ModelState.AddModelError("NewComment", "评论不能为空");
            }
            else
            {
                myService.AddComment(userID, resourceID, NewComment);
            }
            return MovieInfoDetails(resourceID);
        }
        [AcceptVerbs(HttpVerbs.Post), ActionName("MovieIndex")]
        public ActionResult AddMovie(string IMDBInput)
        {
            string result = myService.AddMovie(IMDBInput);
            if (result == "Invalid imdb")
            {
                ModelState.AddModelError("IMDB", "IMDB无效");
                return MovieIndex();
            }
            else
            {
                return MovieIndex();
            }

        }

    }
}
